package com.cheer.ba.view.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.QyeAuthModel;
import com.cheer.ba.presenter.impl.FindOilPresenter;
import com.cheer.ba.utils.Currency;
import com.cheer.ba.utils.ImageUtils;
import com.cheer.ba.utils.Protect;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.dialog.DialogUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 企业认证成功界面
 */
public class QyeAuthAty extends BaseCommAty {

    @BindView(R.id.id_qye_type_txt)
    TextView mTxtQyeType;

    @BindView(R.id.id_qye_logo_img)
    ImageView mImgQyeLogo;

    @BindView(R.id.id_user_account_txt)
    TextView mTxtUserAccount;

    @BindView(R.id.id_qye_name_txt)
    TextView mTxtQyeName;
    @BindView(R.id.id_qye_auth_txt)
    TextView mTxtQyeAuth;

    @BindView(R.id.id_qye_tel_txt)
    TextView mTxtQyeTel;

    private QyeAuthModel model;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_qye, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new FindOilPresenter();
    }

    @Override
    public boolean hasDataInPage() {
        return true;
    }

    @Override
    public void initView() {
        setTitleText("我的账户");
        setTitleRight("取消关注", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.showCommDialog(aty, "取消认证", "取消", "确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtils.DialogDiss();
                        ((FindOilPresenter) presenter).cancelAuth(Session.getUserOrgId());
                    }
                });
                DialogUtils.DialogShow();
            }
        });
        ((FindOilPresenter) presenter).myAuthQye(Session.getUserOrgId());
    }

    @OnClick({R.id.id_qye_tel_txt, R.id.id_look_trade_record_btn})
    public void onClick(View mView) {
        switch (mView.getId()) {
            //拨打电话
            case R.id.id_qye_tel_txt:
                if (null == model || StringUtils.isEmpty(model.orgTel)) {
                    return;
                }
                //拨打电话
                DialogUtils.showCommDialog(aty, "联系电话：" + model.orgTel, "取消", "拨打", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtils.DialogDiss();
                        StringUtils.telPhone(aty, model.orgTel);
                    }
                });
                DialogUtils.DialogShow();
                break;
            //查看交易记录
            case R.id.id_look_trade_record_btn:
//                Intent intent = new Intent(aty, LookMyTradeAty.class);
                Intent intent = new Intent(aty, LookMyBillAty.class);
                intent.putExtra(IntentKey.General.KEY_PAGE, IntentCode.General.JUMP_QYE);
                showActivity(aty, intent);
                break;
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.MY_AUTH_QYE)) {
                //我的认证企业
                model = (QyeAuthModel) t;
                if (null != model) {
                    if ("1".equals(model.orgType)) {
                        mTxtQyeType.setText("供应商");
                    } else if ("1".equals(model.orgType)) {
                        mTxtQyeType.setText("经销商");
                    } else if ("1".equals(model.orgType)) {
                        mTxtQyeType.setText("物流车队");
                    }

                    ImageUtils.showRoundImg(aty, Protect.setPic(model.orgLogo), R.drawable.ic_bg_oil_sale, R.drawable.ic_bg_oil_sale, mImgQyeLogo);

                    mTxtUserAccount.setText(Currency.returnDollar(model.balance).replace("元", ""));

                    mTxtQyeName.setText(model.orgNickName);
                    if (TextUtils.equals("1", model.auditStatus)) {
                        mTxtQyeAuth.setVisibility(View.VISIBLE);
                    }

                    mTxtQyeTel.setText(model.orgTel);
                }
            } else if (TextUtils.equals(tag, ConstantTag.CANCEL_AUTH_QYE)) {
                //取消认证成功
                SVProgressHUD.showSuccessWithStatus(aty, "取消成功");
                Session.setUserOrgId("");
                finish();
            }
        }
    }
}
